package mk.company.fin.model;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
//import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
//import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

import mk.company.fin.FinConstants;

@Entity
@Table(name = FinConstants.Tables.NALOG)
public class Nalog extends Model {

	private static final long serialVersionUID = -1111585097017053394L;

	// persistent
	private Long id;
	private String number;
	private Date date;
	private String description;
	private Year year;
	private List<NalogEntry> nalogEntries;
	
	// transient
	private BigDecimal dolzi;
	private BigDecimal pobaruva;
	private BigDecimal saldo;
	
	
	@Id
	@Column(name = "id_nalog")
	//@SequenceGenerator(name = "nalog_seq", sequenceName = "nalog_seq", allocationSize = 1)
	@GeneratedValue//(strategy = GenerationType.SEQUENCE, generator = "nalog_seq")
	public Long getId() {
		return id;
	}
	
	public void setId(Long id) {
		this.id = id;
	}
	
	@Column(nullable = false)
	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	@Column(nullable = false)
	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	@Column
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	
	@OneToMany(mappedBy = "nalog", orphanRemoval = true, fetch = FetchType.LAZY)
	public List<NalogEntry> getNalogEntries() {
		return nalogEntries;
	}

	public void setNalogEntries(List<NalogEntry> nalogEntries) {
		this.nalogEntries = nalogEntries;
	}
	
	@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	@JoinColumn(name = "id_year", nullable = false)
	public Year getYear() {
		return year;
	}

	public void setYear(Year year) {
		this.year = year;
	}
	

	/**
	 * Calculates the "dolzi", "pobaruva" and "saldo" parts.
	 */
	public void calculateTotals() {
		// init
		dolzi = BigDecimal.ZERO;
		dolzi.setScale(FinConstants.Application.NUMBER_SCALE);
		pobaruva = BigDecimal.ZERO;
		pobaruva.setScale(FinConstants.Application.NUMBER_SCALE);
		
		// calculate
		for (NalogEntry nalogEntry : getNalogEntries()) {
			dolzi.add(nalogEntry.getDolzi());
			pobaruva.add(nalogEntry.getPobaruva());
		}
		
		// saldo
		saldo = dolzi.subtract(pobaruva);
	}

	
	@Transient
	public BigDecimal getDolzi() {
		return dolzi;
	}
	
	@Deprecated
	public void setDolzi(BigDecimal dolzi) {
		throw new UnsupportedOperationException("The field dolzi is dynamic! Use the calculateTotals mehtod!");
	}
	
	@Transient
	public BigDecimal getPobaruva() {
		return pobaruva;
	}
	
	@Deprecated
	public void setPobaruva(BigDecimal pobaruva) {
		throw new UnsupportedOperationException("The field pobaruva is dynamic! Use the calculateTotals mehtod!");
	}
	
	@Transient
	public BigDecimal getSaldo() {
		return saldo;
	}
	
	@Deprecated
	public void setSaldo(BigDecimal saldo) {
		throw new UnsupportedOperationException("The field saldo is dynamic! Use the calculateTotals mehtod!");
	}

}
